
import Vue from 'vue';  
import VueRouter from 'vue-router';  
import Login from '@/components/Login.vue';  
import Register from '@/components/Register.vue';  
import Home from '@/components/Home.vue';  
import DeviceManagement from '@/components/DeviceManagement.vue';  
import DeviceList from '@/components/DeviceList.vue';  
import AlertManagement from '@/components/AlertManagement.vue';  
import AlertRules from '@/components/AlertRules.vue';  
import AlertRecords from '@/components/AlertRecords.vue';  
import SecurityManagement from '@/components/SecurityManagement.vue';  
import SecurityPolicies from '@/components/SecurityPolicies.vue';  
import SecurityLogs from '@/components/SecurityLogs.vue';  
import UserManagement from '@/components/UserManagement.vue';  
import UserList from '@/components/UserList.vue';  
import UserPermissions from '@/components/UserPermissions.vue';  
  
Vue.use(VueRouter);  
  
const routes = [  
  {  
    path: '/login',  
    name: 'Login',  
    component: Login  
  },  
  {  
    path: '/register',  
    name: 'Register',  
    component: Register  
  },  
  {  
    path: '/',  
    name: 'Home',  
    component: Home,  
    meta: { requiresAuth: true } //此路由需要身份验证  
  },  
  {  
    path: '/device-management',  
    name: 'DeviceManagement',  
    component: DeviceManagement,  
    meta: { requiresAuth: true }, // 此路由需要身份验证  
    children: [  
      {  
        path: 'devices',  
        name: 'DeviceList',  
        component: DeviceList  
      }  
    ]  
  },  
  {  
    path: '/alert-management',  
    name: 'AlertManagement',  
    component: AlertManagement,  
    meta: { requiresAuth: true }, // 此路由需要身份验证  
    children: [  
      {  
        path: 'rules',  
        name: 'AlertRules',  
        component: AlertRules  
      },  
      {  
        path: 'records',  
        name: 'AlertRecords',  
        component: AlertRecords  
      }  
    ]  
  },  
  {  
    path: '/security-management',  
    name: 'SecurityManagement',  
    component: SecurityManagement,  
    meta: { requiresAuth: true }, // 此路由需要身份验证  
    children: [  
      {  
        path: 'policies',  
        name: 'SecurityPolicies',  
        component: SecurityPolicies  
      },  
      {  
        path: 'logs',  
        name: 'SecurityLogs',  
        component: SecurityLogs  
      }  
    ]  
  },  
  {  
    path: '/user-management',  
    name: 'UserManagement',  
    component: UserManagement,  
    meta: { requiresAuth: true }, // 此路由需要身份验证  
    children: [  
      {  
        path: 'users',  
        name: 'UserList',  
        component: UserList  
      },  
      {  
        path: 'permissions',  
        name: 'UserPermissions',  
        component: UserPermissions  
      }  
    ]  
  },  
  // 如果路径不匹配任何已定义的路由，则重定向到登录页面  
  {  
    path: '*',  
    redirect: '/login'  
  }  
];  
  
const router = new VueRouter({  
  mode: 'history',  
  base: process.env.BASE_URL,  
  routes  
});  
  
// 导航守卫示例：检查是否需要身份验证  
router.beforeEach((to, from, next) => {  
  const requiresAuth = to.matched.some(record => record.meta.requiresAuth);  
  const isLoggedIn =  localStorage.setItem('logintoken') /* 登录状态检查逻辑，localStorage */;  
  
  if (requiresAuth && !isLoggedIn) {  
    next('/login'); // 如果需要身份验证但用户未登录，则重定向到登录页面  
  } else {  
    next(); // 否则，继续直接导航 
  }  
});  
  
export default router;